package com.example.http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class HttpApp {

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test", httpExchange -> {
            String name="hello word";
            httpExchange.getResponseHeaders().add("content-type","text/html;charset=utf-8");
            httpExchange.sendResponseHeaders(200,name.length());
            OutputStream os = httpExchange.getResponseBody();
            os.write(name.getBytes());
            os.close();
        });
        server.setExecutor(null);
        server.start();
        System.out.println("启动完毕");
    }
}
